package com.hpe.aruba.CPPM.dialogs;

import android.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hpe.aruba.CPPM.ManageMacsActivity;
import com.hpe.aruba.CPPM.R;
import com.hpe.aruba.CPPM.objects.Device;

import java.util.ArrayList;

public class SortDialog {

    private final AlertDialog dialogBuilder;
    private final View dialogView;
    private ManageMacsActivity context;

    private ArrayList<Device> staticMacs;


    static int sortRadioNumber;
    static boolean vendorBoxChecked;

    private CheckBox checkBoxVendor;

    private RadioGroup sortRadioGroup;
    private RadioButton radio_mac;
    private RadioButton radio_date;
    private RadioButton radio_vendor;



    private Button button1;
    private Button button2;


    public SortDialog (ManageMacsActivity context, ArrayList<Device> macs) {
        this.context = context;

        dialogBuilder = new AlertDialog.Builder(context).create();
        LayoutInflater inflater = context.getLayoutInflater();
        dialogView = inflater.inflate(R.layout.sort_dialog_guest, null);

        staticMacs = new ArrayList<>();
        initFields(macs);

    }

    private void initFields(ArrayList<Device> macs) {
        button1 = dialogView.findViewById(R.id.buttonSubmit);
        button2 = dialogView.findViewById(R.id.buttonCancel);

        checkBoxVendor = dialogView.findViewById(R.id.checkBox_vendor);
        checkBoxVendor.setChecked(vendorBoxChecked);

        sortRadioGroup = dialogView.findViewById(R.id.sort_radioGroup);
        radio_mac = dialogView.findViewById(R.id.radio_mac);
        radio_date = dialogView.findViewById(R.id.radio_date);
        radio_vendor = dialogView.findViewById(R.id.radio_vendor);
        /*Sets the lastSort as the pre-selected Radio*/
        setPreSelectedSortRadio();

        button1.setOnClickListener(view -> {
            handleSelections(macs);
            dialogBuilder.dismiss();
        });
        button2.setOnClickListener(view -> dialogBuilder.dismiss());
    }

    /**Only shows the Status-filter RadioGroup, if the Box for the status-filter is checked
     */
    private void setRadioGroupVisibility(RadioGroup radioGroup, boolean bool){
        radioGroup.setVisibility(bool ? radioGroup.VISIBLE : radioGroup.GONE);
    }

    /**Sets sort radioNumber depending on the radio button that was selected.
     * sortRadioNumber is set according to the cases in ManageMacsActivity -> sortList(n)
     */
    private void setSortRadioNumber(RadioGroup radioGroup){
        switch(radioGroup.getCheckedRadioButtonId()){
            case R.id.radio_mac:
                sortRadioNumber = 0;
                break;
            case R.id.radio_date:
                sortRadioNumber = 1;
                break;
            case R.id.radio_vendor:
                sortRadioNumber = 2;
                break;
        }
    }

    private void setPreSelectedSortRadio(){
        switch (context.getLastSort()){
            case 0:
                radio_mac.setChecked(true);
                break;
            case 1:
                radio_date.setChecked(true);
                break;
            case 2:
                radio_vendor.setChecked(true);
                break;
        }
    }


    /**Performs filtering. Only Macs that have a vendor are shown.
     * TODO: Save all the deleted macs to undo the filter if unchecked*/
    private void filterHasVendor(ArrayList<Device> macs){
        staticMacs.addAll(context.getStaticMacList());
        macs.clear();
        for(Device mac : staticMacs){
            if (mac.getVendor() != null && !mac.getVendor().equals("")){
                macs.add(mac);
            } else {
                context.vendorFilterRest.add(mac);
                Log.d("xxxxxx", "vendorFilterRest is: " + context.vendorFilterRest);
            }
        }
        context.updateMac();
    }


    /**save all the checkBoxes and radioButtons states handle the functionality of the checked boxes*/
    private void handleSelections(ArrayList<Device> macs){
        if(checkBoxVendor.isChecked()){
            vendorBoxChecked = true;
            filterHasVendor(macs);
        } else {
            vendorBoxChecked = false;
            macs.addAll(context.vendorFilterRest);
            Log.d("xxxxxxxxxxx", "vendorFilterRest was added to macList: " + context.vendorFilterRest);
            context.vendorFilterRest.clear();
            context.updateMac();
        }

        //if(!checkBoxNetwork.isChecked() && !checkBoxStatus.isChecked() && !checkBoxVendor.isChecked()){ context.resetMacs();}
        /**TODO: Refresh Mac List based on sort here - Instead of resetting all just remember the items that are no longer in the list*/

        /*Sorts the Mac List depending on the sortRadio that was selected*/
        setSortRadioNumber(sortRadioGroup);
        context.sortList(sortRadioNumber);
    }


    public void onCheckboxClicked(View view){

        switch (view.getId()){
            case R.id.checkBox_vendor:
                boolean checked = ((CheckBox) view).isChecked();
                if (checked){
                    vendorBoxChecked = true;
                } else {
                    vendorBoxChecked = false;
                }
                break;
            case R.id.checkBox_vendor_row:
                CheckBox vendorBox = dialogView.findViewById(R.id.checkBox_vendor);
                vendorBox.toggle();
                onCheckboxClicked(vendorBox);
                break;
        }

    }


    public void show() {
        dialogBuilder.setView(dialogView);
        dialogBuilder.show();
    }
}


